﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.ProductInfo;

namespace com.subb.ProductInformation
{
    public class PP_ProductImgDAL
    {
        public List<PP_ProductImg> GetAllProductImg(int prodId, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.PP_ProductImg.Where(c => c.ProductID == prodId).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ID);
                ttlCount = query.Count();
                List<PP_ProductImg> imglist = query.Skip(start).Take(limit).ToList();
                return imglist;
            }
        }

        public List<PP_ProductImg> GetAllProductImg(int prodId)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.PP_ProductImg.Where(c => c.ProductID == prodId).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ID);
                List<PP_ProductImg> imglist = query.ToList();
                return imglist;
            }
        }

        public PP_ProductImg GetProductImg(int id)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                PP_ProductImg img = ctx.PP_ProductImg.SingleOrDefault(c => c.ID == id);
                return img;
            }
        }

        public int CreateProductImg(ProductImg v_prodImg)
        {
            int id = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                PP_ProductImg ppimg = new PP_ProductImg
                {
                    ProductID = v_prodImg.ProductID,
                    Title = v_prodImg.Title,
                    ImgUrl = v_prodImg.ImgUrl,
                    SortNum = v_prodImg.SortNum,
                    Description = v_prodImg.Description,
                    CreateTime = DateTime.Now,
                    Creator = v_prodImg.Creator
                };
                ctx.PP_ProductImg.AddObject(ppimg);
                ctx.SaveChanges();
                id = ppimg.ID;
            }
            return id;
        }

        public bool UpdateProductImg(int id, ProductImg v_prodImg)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                PP_ProductImg ppimg = ctx.PP_ProductImg.SingleOrDefault(c => c.ID == id);
                if (ppimg != null)
                {
                    ppimg.ProductID = v_prodImg.ProductID;
                    ppimg.Title = v_prodImg.Title;
                    ppimg.ImgUrl = v_prodImg.ImgUrl;
                    ppimg.SortNum = v_prodImg.SortNum;
                    ppimg.Description = v_prodImg.Description;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveProductImg(int id)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                PP_ProductImg ppimg = ctx.PP_ProductImg.SingleOrDefault(c => c.ID == id);
                if (ppimg != null)
                {
                    ctx.PP_ProductImg.DeleteObject(ppimg);
                    ctx.SaveChanges();
                }
            }
            return result;
        }
    }
}
